package com.primaryedu.ui.app.classadjust;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.primaryedu.R;
import com.primaryedu.data.Contants;
import com.primaryedu.data.GlobalData;
import com.primaryedu.dialog.CustomDialog;
import com.primaryedu.ui.BaseActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Thomas on 2018/3/6.
 */

public class ClassApproveActivtiy extends BaseActivity implements View.OnClickListener {

    private TextView tv_leavetype_approve;
    private TextView tv_leavereanson;
    private RadioGroup radioGroup;
    private String opinionStatus = "2";
    private String approveId;
    private CustomDialog.Builder ibuilder;
    private Button bt_submit_approve;
    private RadioButton rb_agree_approve;
    private RadioButton rb_disagree_approve;
    private static final int REQUEST_PREVIEW_CODE = 20;
    private TextView tv_approve_auditorName;
    private String id;
    private TextView text_remark;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_class_approve);

        init();
    }

    @SuppressLint("NewApi")
    private void init() {
        Intent intent = getIntent();
        //调课信息
        String remarks = intent.getStringExtra("remarks");
        //审批人
        String auditorname = intent.getStringExtra("auditorname");
        //课务ID
        id = intent.getStringExtra("id");
        //状态处理
        String classStaus = intent.getStringExtra("staus");
        Log.i("yy", "init: classStaus=====" + classStaus);
        Log.i("yy", "init: approveId=====" + approveId);

        ((TextView) findViewById(R.id.title)).setText("调课审批");
        findViewById(R.id.back).setOnClickListener(this);


        //调课信息
        text_remark = (TextView) findViewById(R.id.text_remark);
        text_remark.setText(remarks);
        //审批人
        tv_approve_auditorName = (TextView) findViewById(R.id.tv_approve_auditorName);
        Log.i("yy", "init: auditorname=====" + GlobalData.getUsername(this));
        tv_approve_auditorName.setText(GlobalData.getUsername(this));
        //审批意见
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new ClassApproveActivtiy.OpinionRadioButtonListener());//给RadioGroup绑定监视器

        rb_agree_approve = (RadioButton) findViewById(R.id.rb_agree_approve);
        rb_disagree_approve = (RadioButton) findViewById(R.id.rb_disagree_approve);
        //提交
        bt_submit_approve = (Button) findViewById(R.id.bt_submit_approve);
        findViewById(R.id.bt_submit_approve).setOnClickListener(this);

        ImageView image = (ImageView) findViewById(R.id.image_approve_class);
        if (classStaus.equals("2")) {//请假审批同意
            image.setBackground(this.getResources().getDrawable(R.drawable.image_agree));
            disableRadioGroup(radioGroup);
            rb_agree_approve.setChecked(true);
            //提交按钮不可见
            bt_submit_approve.setVisibility(View.GONE);
        } else if (classStaus.equals("1")) {//未处理
            image.setBackground(this.getResources().getDrawable(R.drawable.leave_no_deal));
        } else if (classStaus.equals("3")) {//请假审批不同意
            image.setBackground(this.getResources().getDrawable(R.drawable.image_disagree));
            disableRadioGroup(radioGroup);
            rb_disagree_approve.setChecked(true);
            //提交按钮不可见
            bt_submit_approve.setVisibility(View.GONE);
        }
    }

    @Override
    protected int BindLayout() {
        return R.layout.activity_class_approve;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    public void disableRadioGroup(RadioGroup testRadioGroup) {
        for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
            testRadioGroup.getChildAt(i).setEnabled(false);
        }
    }

    class OpinionRadioButtonListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // 选中状态改变时被触发
            switch (checkedId) {
                case R.id.rb_agree_approve:
                    // 当用户选择同意时
                    opinionStatus = "2";
//					showToast(opinionStatus);
                    break;
                case R.id.rb_disagree_approve:
                    // 当用户选择不同意时
                    opinionStatus = "3";
//					showToast(opinionStatus);
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.bt_submit_approve:
                showApplyleavetrue();
                break;
            default:
                break;
        }

    }

    private void requestLeaveApprovelist() {
        showLoading("提交中...");
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Contants.classApprove,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        dismissLoading();
                        System.out.println("response=leavelist=" + response);

                        try {
                            JSONObject json = new JSONObject(response);
                            int result = json.getInt("result");

                            if (result == 1) {
                                showToast("审批成功");
                                Intent intent = getIntent();
                                setResult(3, intent);
                                finish();
                                String data = json.getString("data");
                                JSONObject listObj = new JSONObject(data);
                                String listStr = listObj.getString("list");

//								leaveLists  = new Gson().fromJson(listStr,
//										new TypeToken<List<LeavelistBean.DataBean.ListBean>>() {
//										}.getType());
//								if (uploadMore) {
//									loadmoreList.addAll(leaveLists);
//									handler.sendEmptyMessage(1002) ;
//									uploadMore = false;
//								}else {
//									leavetempList.addAll(leaveLists);
//									handler.sendEmptyMessage(1001) ;
//								}

                                System.out.println("leaveLists==" + listStr);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                errorLoading("提交失败，请重新提交");
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                //在这里设置需要post的参数
                Map<String, String> map = new HashMap<String, String>();
                map.put("id", id);
                map.put("status", opinionStatus);
                map.put("auditorId", GlobalData.getUserId(ClassApproveActivtiy.this));
                map.put("auditorName", GlobalData.getUsername(ClassApproveActivtiy.this));
                return map;
            }
        };
        requestQueue.add(stringRequest);
    }

    private void showApplyleavetrue() {
        ibuilder = new CustomDialog.Builder(ClassApproveActivtiy.this);
        ibuilder.setTitle("请假");
        ibuilder.setMessage("确定审批吗？");
        ibuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

            }
        });

        ibuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                requestLeaveApprovelist();
            }
        });

        Dialog dialog = ibuilder.create();
        dialog.show();
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
        android.view.WindowManager.LayoutParams p = dialog.getWindow().getAttributes();  //获取对话框当前的参数值
        p.width = (int) (d.getWidth() * 0.8);    //宽度设置为屏幕的0.9
        dialog.getWindow().setAttributes(p);     //设置生效
    }
}
